package org.graduation.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.graduation.common.ServiceException;
import org.graduation.feignapi.VerifyCodeClient;
import org.graduation.model.common.ResponseResult;
import org.graduation.model.common.user.User;
import org.graduation.model.common.verifyCode.VerifyCode;
import org.graduation.model.dtos.RegisterDTO;
import org.graduation.user.mapper.MybatisPlusWrapperFactory;
import org.graduation.user.mapper.UserMapper;
import org.graduation.user.service.UserService;
import org.graduation.utils.Encrypt;
import org.graduation.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private UserMapper userMapper;

    @Autowired
    private Token token;

    @Autowired
    private VerifyCodeClient verifyCodeClient;

    @Value("${spring.application.name}")
    private String applicationName;

    private Encrypt encrypt;

    @Override
    public ResponseResult login(User user) throws ServiceException {
        // 验证账号、密码
        user.setPassword(encrypt.md5_32(user.getPassword()));
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("email", user.getEmail()).eq("password", user.getPassword());
        user = userMapper.selectOne(wrapper);
        if (Objects.isNull(user)) {
            return ResponseResult.failed("邮箱或密码错误");
        }
        user.hiddenPassword();
        String t = token.generateToken(user);
        // 返回
        return ResponseResult.success(t);
    }

    @Override
    public void logout(String token) {

    }

    @Override
    public ResponseResult swapToken(String token) {
        return null;
    }

    @Override
    public ResponseResult register(RegisterDTO registerDTO) throws ServiceException {
        if (StringUtils.isEmpty(registerDTO.getEmail())) {
            throw new ServiceException("邮箱为空");
        } else if (userMapper.exists(new LambdaQueryWrapper<User>().eq(User::getEmail, registerDTO.getEmail()))) {
            return ResponseResult.failed("邮箱已被注册");
        }
        if (StringUtils.isEmpty(registerDTO.getPassword())) {
            throw new ServiceException("密码为空");
        } else {
            String encryptPassword = encrypt.md5_32(registerDTO.getPassword());
            registerDTO.setPassword(encryptPassword);
        }
        if (StringUtils.isEmpty(registerDTO.getNickname())) {
            registerDTO.setNickname(registerDTO.getEmail());
        }
        if (StringUtils.isEmpty(registerDTO.getVerifyCode())) {
            return ResponseResult.failed("验证码为空");
        }
        // 验证验证码
        VerifyCode verifyCode = new VerifyCode(applicationName,
                registerDTO.getEmail(), registerDTO.getVerifyCode().toUpperCase(), 300);
        ResponseResult tempResponse = verifyCodeClient.verifyCode(verifyCode);
        if (tempResponse.getCode() < 0) {
            return tempResponse;
        }
        User temp = new User();
        temp.setEmail(registerDTO.getEmail());
        temp.setNickname(registerDTO.getNickname());
        temp.setPassword(registerDTO.getPassword());
        userMapper.insert(temp);
        temp.hiddenPassword();
        return ResponseResult.success(temp);
    }

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Autowired
    public void setEncrypt(Encrypt encrypt) {
        this.encrypt = encrypt;
    }

    @Override
    public ResponseResult getInfoByEmail(String email) {
        User user = userMapper.selectOne(MybatisPlusWrapperFactory.getInfoByEmail(email));
        return ResponseResult.success(user);
    }
}
